package com.ctf.soft.ms.controls.manage;

import com.ctf.soft.ms.dao.dto.manage.Pager;
import com.ctf.soft.ms.dao.model.manage.Menu;
import com.ctf.soft.ms.dao.model.manage.MenuRelation;
import com.ctf.soft.ms.service.manage.IMenuService;
import com.ctf.soft.ms.utils.AnnotationUtil;
import com.ctf.soft.ms.utils.ToolsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.persistence.Column;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 名称： MenuController<br>
 * 描述： 系统菜单控制器<br>
 * 最近修改时间：2015/7/31 13:39<br>
 * 修改说明：
 *
 * @author jiajl
 */
@Controller
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private IMenuService menuService;

    @RequestMapping("/main")
    public String doMain() {
        return "manage/menu/main";
    }

    @RequestMapping("/tree")
    public String doTree() {
        return "manage/menu/tree";
    }

    @RequestMapping("/view")
    public String doView() {
        return "manage/menu/view";
    }

    @RequestMapping("/editor")
    public String doEditor() {
        return "manage/menu/editor";
    }

    @RequestMapping("/SaveOrUpdate")
    @ResponseBody
    public String doSaveOrUpdate(@RequestBody Menu menu,
                                 HttpServletRequest request) {
        String result = "faild";
        try {
            int c = -1;
            if (menu.getId() == null || menu.getId().trim().equals("")) {
                menu.setId(ToolsUtil.getRandomForUUID());
                c = menuService.insertSelective(menu);
            } else {
                c = menuService.updateByPrimaryKeySelective(menu);
            }
            if (c > 0) {
                result = "success";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("/Delete")
    @ResponseBody
    public String doDelete(@RequestParam("id") String id,
                           HttpServletRequest request) {
        String result = "faild";
        try {
            int c = -1;
            Menu menu = new Menu();
            menu.setId(id);
            menu.setStatus("D");
            c = menuService.updateByPrimaryKeySelective(menu);
            if (c > 0) {
                result = "success";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("/Data")
    @ResponseBody
    public Menu loadData(@RequestParam("id") String id,
                         HttpServletRequest request) {
        Menu menu = null;
        try {
            menu = menuService.selectByPrimaryKey(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return menu;
    }

    @RequestMapping("/List")
    @ResponseBody
    public List<Menu> loadList(HttpServletRequest request) {
        List<Menu> list = null;
        try {
            Menu params = new Menu();
            if (!ToolsUtil.isEmpty(request.getParameter("pid"))) {
                params.setParentId(request.getParameter("pid").toString().trim());
            }
            params.setStatus("Y");
            list = menuService.selectByList(params);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    @RequestMapping("/PageList")
    @ResponseBody
    public Pager<Menu> loadPageList(@RequestBody ModelMap params,
                                    HttpServletRequest request) {
        Pager<Menu> pager = null;
        try {
            params.put("status", "D");
            String sidx = (String) params.get("sidx");
            String sord = (String) params.get("sord");
            params.put("sord", sord);
            params.put("sidx", AnnotationUtil.getColumnValue(Menu.class, Column.class, sidx, "name"));

            int totalRecord = menuService.getPageCount(params);
            int page = Integer.valueOf(params.get("page").toString());
            int rows = Integer.valueOf(params.get("rows").toString());
            int totalPage = totalRecord % rows == 0 ? totalRecord / rows
                    : totalRecord / rows + 1;
            List<Menu> list = menuService.loadPageList(params);
            if (list != null) {
                pager = new Pager<Menu>();
                pager.setCurrPage(page);
                pager.setTotalPage(totalPage);
                pager.setTotalRecord(totalRecord);
                pager.setList(list);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pager;
    }

    @RequestMapping("/Relation")
    @ResponseBody
    public List<MenuRelation> loadRelation(
            @RequestParam("id") String code, HttpServletRequest request) {
        List<MenuRelation> list = null;
        try {
            list = menuService.loadListByRelation("",code);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
}
